package com.example.demo.dao.daoimpl;

import com.example.demo.dao.RoleMapper;
import com.example.demo.pojo.Role;
import com.example.demo.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.BeanPropertySqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.sql.DataSource;
import java.util.List;

/**
 * <h3>reqister</h3>
 * <p>查询角色</p>
 *
 * @author : chenjunjie
 * @date : 2021-06-24 08:48
 **/
@Repository
public class RoleMapperImpl implements RoleMapper {


    @Autowired
    DataSource dataSource;

    @Autowired
    JdbcTemplate template;

    @Autowired
    NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public List<Role> findRoleList() {
        List<Role> roleList = template.query("select role_id,role_name,status from role", new Role());
        return roleList;
    }

    @Override
    public boolean deleteRole(Integer roleId) {
        int update = template.update("delete from role where role_id = ?", roleId);
        return update>0?true:false;
    }

    @Override
    public boolean updateRole(RoleVo roleVo) {
        StringBuffer bufferSql=new StringBuffer("UPDATE sys_authority join role on role.role_id=sys_authority.role_id " +
                "and role.role_id=:roleId SET role_name=:roleName,status =:status," +
                " is_check = ( CASE ");

        for (Integer id : roleVo.getRoleMenuId()) {
            String sql=" WHEN id = "+id+" THEN 0";
            bufferSql.append(sql);
        }
        bufferSql.append(" END ) WHERE id IN ( :roleMenuId)");
        System.out.println(bufferSql.toString());
       namedParameterJdbcTemplate.update(bufferSql.toString(),new BeanPropertySqlParameterSource(roleVo));



        return false;
    }
}
